import * as correlateService from '../../services/limit/correlateService';
import * as roleService from '../../services/user/roleService';
import * as applicationService from '../../services/organize/applicationService';
import * as departmentService from '../../services/organize/departmentService';
import * as postService from '../../services/organize/postService';
import * as userService from '../../services/user/userService';
import { message } from "antd";
import config from '../../config/config';

/**
 * 部门管理Model
 */
export default {
  namespace: "correlateModel",
  state: {
    bizType: "correlate",
    departmentLoading: false,
    departmentList: [],
    limitList: [],
    total: 0,
    currentPage: 1,
    pageSize: config.PAGE_SIZE,
    departmentModalVisible: false,
    operateType: 'add',
    departmentInfoData: null,
    selectedRowKeys: [],
    searchParams: null,
    correlateVos: [],
    correlateList: [],
    userList: [],
    relateList: [],
    correlateType: "0",
    relateTransferVisible: false,
    parentRecord: null,
  },

  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload }
    },
  },

  effects: {
    *initOrganize({ payload: params }, {select, call, put}) {
      yield put({ type: "updateState", payload: { correlateType: "2" }});
      yield put({ type: 'getUnitNodeTree', payload: { type: "2" }});
      yield put({ type: 'getCorrelatePage', payload: { type: "2" }});
    },

    *initUser({ payload: params }, {select, call, put}) {
      yield put({ type: "updateState", payload: { correlateType: "6" }});
      yield put({ type: 'getAppRoleTree', payload: { type: "2" }});
      yield put({ type: 'getCorrelatePage', payload: { type: "6" }});
    },

    *getUnitNodeTree({ payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { departmentLoading: true }});
      let res = yield call(correlateService.getUnitNodeTree, params);
      if (res && res.code == 200) {
        yield put({ type: "updateState", payload: { limitList: res.data }});
      }
      yield put({ type: "updateState", payload: { departmentLoading: false }});
    },

    *getAppRoleTree({ payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { departmentLoading: true }});
      let res = yield call(correlateService.getAppRoleTree, params);
      if (res && res.code == 200) {
        yield put({ type: "updateState", payload: { limitList: res.data }});
      }
      yield put({ type: "updateState", payload: { departmentLoading: false }});
    },

    *getCorrelateList({ payload: params }, { select, call, put }) {
      let res = yield call(correlateService.getCorrelateList, { ...params });
      if (res.code == "200") {
        yield put({ type: 'updateState', payload: { correlateVos: res.data }});
      }
    },

    *getCorrelatePage({ payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { departmentLoading: true }});
      const { currentPage, pageSize } = yield select(state => state.correlateModel);
      let res = yield call(correlateService.getCorrelatePage, { ...params, currentPage, pageSize });
      if (res.code == "200") {
        yield put({ type: 'updateState', payload: { correlateList: res.data.records, total: res.data.total }});
      }
      yield put({ type: "updateState", payload: { departmentLoading: false }});
    },

    *getRoleList({payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { departmentLoading: true }});
      const res = yield call(roleService.getRoleList, { ...params });
      if (res.code == "200") {
        let payload = params?.type && params.type == "limit" ? { limitList: res.data } : { relateList: res.data };
        yield put({ type: 'updateState', payload });
      }
      yield put({ type: "updateState", payload: { departmentLoading: false }});
    },

    *getApplicationList({payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { departmentLoading: true }});
      const res = yield call(applicationService.getApplicationList, { ...params });
      if (res.code == "200") {
        yield put({ type: 'updateState', payload: { relateList: res.data }});
      }
      yield put({ type: "updateState", payload: { departmentLoading: false }});
    },

    *getCorrelateTeanatAndAppByUser({payload: params }, { select, call, put }) {
      let userJson = sessionStorage.getItem("userInfo");
      let userObj = JSON.parse(userJson);
      const res = yield call(correlateService.getCorrelateTeanatAndAppByUser, { userId: userObj.userId });
      if (res.code == "200") {
        let userRoleFlag = "0" == userObj.roleType;
        let limitList = [], tenantList = res?.data?.tenantList, appList = res?.data?.appList;
        for (let i = 0; i < tenantList.length; i++) {
          let record = tenantList[i], appChildren = null;
          if (userRoleFlag) {
            appChildren = appList.filter(item => record.id == item.targetId);
            record.children = appChildren;
            limitList.push(record);
          } else if (userObj.tenantId == record.id) {
            appChildren = appList.filter(item => res.data.tenantId == item.targetId);
            record.children = appChildren;
            limitList.push(record);
          }
        }
        yield put({ type: 'updateState', payload: { limitList }});
      }
    },

    *getDepartmentList({payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { departmentLoading: true }});
      const res = yield call(departmentService.getDepartmentList, { ...params });
      if (res.code == "200") {
        yield put({ type: 'updateState', payload: { relateList: res.data }});
      }
      yield put({ type: "updateState", payload: { departmentLoading: false }});
    },

    *getPostList({payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { departmentLoading: true }});
      const res = yield call(postService.getPostList, { ...params });
      if (res.code == "200") {
        yield put({ type: 'updateState', payload: { relateList: res.data }});
      }
      yield put({ type: "updateState", payload: { departmentLoading: false }});
    },

    *getUserList({payload: params }, { select, call, put }) {
      yield put({ type: "updateState", payload: { departmentLoading: true }});
      const res = yield call(userService.getUserList, { ...params });
      if (res.code == "200") {
        yield put({ type: 'updateState', payload: { relateList: res.data }});
      }
      yield put({ type: "updateState", payload: { departmentLoading: false }});
    },

    *addCorrelate({ payload: params }, {put, call}) {
      const res = yield call(correlateService.addCorrelate, params);
      if (res.code == "200") {
        message.info("新增成功！");
        yield put({ type: 'updateState', payload: { departmentModalVisible: false }});
        yield put({ type: 'getCorrelatePage', payload: { ...params }});
      } else {
        message.info("新增失败！");
      }
    },

    *deleteCorrelate({ payload: params }, {put, call}) {
      const res = yield call(correlateService.deleteCorrelate, params);
      if (res.code == "200") {
        message.info("删除成功！");
        yield put({ type: 'getCorrelatePage', payload: params });
      } else {
        message.info("删除失败！");
      }
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      const pathname = window.location.pathname;
      console.log("correlateModel subscriptions setup pathname => ", pathname);
      // 组织管理/关联管理
      if (pathname === "/organize/relate") {
        dispatch({ type: 'initOrganize', payload: {}});
      }

      // 权限管理/关联管理
      if (pathname === "/user/correlate") {
        dispatch({ type: 'initUser', payload: {}});
      }
    },
  },
}
